//
//  SearchViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "SearchViewController.h"
#import "PublicHeader.h"
#import "FCollectionReusableView.h"
#import "WHNCollectionViewCell.h"
#import "ResultViewController.h"
#import "FSearchViewController.h"
#import "ILButton.h"

@interface SearchViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, assign)BOOL isHistory;
@property(nonatomic, strong)NSMutableArray *historyArray;
@property(nonatomic, strong)NSMutableArray *hotArray;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *searchArray;
@end
static NSString * const header = @"Header";
static NSString * const reuseIdentifier = @"Cell";

@implementation SearchViewController

#pragma mark 设置collectionView
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _searchArray = [NSMutableArray array];
    
    [self.collectionView NightWithType:UIViewColorTypeNormal];
    [self.collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[FCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
    [self initData];
    
    //搜索透视图
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"🔍 请输入搜索内容" forState:UIControlStateNormal];
    [button setTitleColor:[ThemeManage shareThemeManage].textColorWhite forState:UIControlStateNormal];
//    button.titleLabel.textType = @1;
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAcion) forControlEvents:UIControlEventTouchUpInside];
//    button.lineBreakMode = NSLineBreakByTruncatingMiddle;
    self.navigationItem.titleView = button;
    // Do any additional setup after loading the view.
}

-(void)buttonAcion
{
    FSearchViewController *vc = [[FSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
}

#pragma mark - 初始化数据
-(void)initData
{
    _hotArray = [NSMutableArray array];
    
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/m/hot_search_keys?device=iPhone" WithDataBlock:^(id data) {
        for (NSString *each in [data objectForKey:@"keys"]) {
            [_hotArray addObject:each];
            
        }
        [_collectionView reloadData];
    }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    if (_isHistory) {
//        return 2;
//    }
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if (_isHistory) {
//        if (section == 0) {
//            //            return _historyArray;
//            return 1;
//        }
//    }
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.layer.cornerRadius = 5;
//    cell.kind = WHNCollectionViewKindOneLabel;
//    if (_isHistory) {
//        if (indexPath.section == 0) {
//            cell.titleLabel.text = @"历史";
//            return cell;
//        }
//    }
//    
//    cell.titleLabel.text = @"热门";
    cell.kind = WHNCollectionViewKindOneLabel;
    if (_hotArray.count != 0) {
        cell.titleLabel.text = _hotArray[indexPath.row];
    }
    // Configure the cell
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(FitConstant_XorWidth(100), FitConstant_YorHeight(40));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, FitConstant_YorHeight(40));
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:header forIndexPath:indexPath];
    view.type = FCollectionReusableViewTypesSearch;
    if (_isHistory) {
        if (indexPath.section == 0) {
            view.titleLabel.text = @"历史搜索";
            return view;
        }
    }
    view.titleLabel.text = @"热门搜索";
    return view;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ResultViewController *vc = [[ResultViewController alloc] init];
    vc.searchString = _hotArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
